package avicit.bdp.oss.utils.upload.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.web.multipart.MultipartFile;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2020-12-09
 * @类说明：UploadDto
 * @修改记录：
 * @注意事项：
 * @主要功能：断点续传/分片上传公共类
 */
public class UploadDto {
    /**
     * 分片文件，JSON序列化时忽略
     */
    @JsonIgnore
    private MultipartFile file;

    /**
     * 文件唯一标识
     */
    private String fileMd5;

    /**
     * 是否断点续传
     */
    private Boolean allowResume;

    /**
     * 上传进度
     */
    private String progress;

    /**
     * 当前分片在上传分片中的顺序（从0开始）
     */
    private Integer chunkNo;

    /**
     * 总分片数
     */
    private Integer chunks;

    /**
     * 每个文件块的大小
     *
     */
    private Long chunkSize;

    public UploadDto() {
    }

    public <T extends UploadDtoBuilder> UploadDto(UploadDtoBuilder builder) {
        this.file = builder.file;
        this.fileMd5 = builder.fileMd5;
        this.allowResume = builder.allowResume;
        this.progress = builder.progress;
        this.chunkNo = builder.chunkNo;
        this.chunks = builder.chunks;
        this.chunkSize = builder.chunkSize;
    }

    public static UploadDtoBuilder builder() {
        return new UploadDtoBuilder();
    }

    public MultipartFile getFile() {
        return file;
    }

    public String getFileMd5() {
        return fileMd5;
    }

    public Boolean getAllowResume() {
        return allowResume;
    }

    public String getProgress() {
        return progress;
    }

    public Integer getChunkNo() {
        return chunkNo;
    }

    public Integer getChunks() {
        return chunks;
    }

    public Long getChunkSize() {
        return chunkSize;
    }

    public void setFile(MultipartFile file) {
        this.file = file;
    }

    public void setFileMd5(String fileMd5) {
        this.fileMd5 = fileMd5;
    }

    public void setAllowResume(Boolean allowResume) {
        this.allowResume = allowResume;
    }

    public void setProgress(String progress) {
        this.progress = progress;
    }

    public void setChunkNo(Integer chunkNo) {
        this.chunkNo = chunkNo;
    }

    public void setChunks(Integer chunks) {
        this.chunks = chunks;
    }

    public void setChunkSize(Long chunkSize) {
        this.chunkSize = chunkSize;
    }

    /**
     * 在父类中使用泛型 根据调用的子类不同 返回子类对应的Builder
     */
    public static class UploadDtoBuilder<T extends UploadDtoBuilder> {
        private MultipartFile file;
        private String fileMd5;
        private Boolean allowResume;
        private String progress;
        private Integer chunkNo;
        private Integer chunks;
        private Long chunkSize;

        public T file(MultipartFile file) {
            this.file = file;
            return (T) this;
        }

        public T fileMd5(String fileMd5) {
            this.fileMd5 = fileMd5;
            return (T) this;
        }

        public T allowResume(Boolean allowResume) {
            this.allowResume = allowResume;
            return (T) this;
        }

        public T progress(String progress) {
            this.progress = progress;
            return (T) this;
        }

        public T chunkNo(Integer chunkNo) {
            this.chunkNo = chunkNo;
            return (T) this;
        }

        public T chunks(Integer chunks) {
            this.chunks = chunks;
            return (T) this;
        }

        public T chunkSize(Long chunkSize) {
            this.chunkSize = chunkSize;
            return (T) this;
        }
    }
}
